d8049a
@@ -18,33 +18,79 @@
package io.fabric8.agent.download;
 
 import java.io.File;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.URL;
 import java.util.concurrent.ExecutorService;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 public class SimpleDownloadTask extends AbstractDownloadTask {
 
+    /**
+     * Logger.
+     */
+    private static final Logger LOG = LoggerFactory.getLogger(AbstractDownloadTask.class);
+    /**
+     * 2 spaces indent;
+     */
+    private static final String Ix2 = "  ";
+    /**
+     * 4 spaces indent;
+     */
+    private static final String Ix4 = "    ";
+    
     public SimpleDownloadTask(String url, ExecutorService executor) {
         super(url, executor);
     }
 
     @Override
     protected File download() throws Exception {
-        File dir = new File(System.getProperty("karaf.data"), "fabric-agent");
-        dir.mkdirs();
-        File tmpFile = File.createTempFile("download-", null, dir);
-        InputStream is = new URL(url).openStream();
         try {
-            OutputStream os = new FileOutputStream(tmpFile);
+            LOG.trace("Downloading [" + url + "]");
+            
+            URL urlObj = new URL(url);
+            File file = new File(urlObj.getFile());
+            if (file.exists()) {
+                return file;
+            }
+
+            File dir = new File(System.getProperty("karaf.data"), "fabric-agent");
+            dir.mkdirs();
+            if (!dir.isDirectory()) {
+                throw new IOException("Unable to create directory " + dir.toString());
+            }
+
+            File tmpFile = File.createTempFile("download-", null, dir);
+            
+            InputStream is = urlObj.openStream();
             try {
-                copy(is, os);
+                OutputStream os = new FileOutputStream(tmpFile);
+                try {
+                    copy(is, os);
+                } finally {
+                    os.close();
+                }
             } finally {
-                os.close();
+                is.close();
             }
-        } finally {
-            is.close();
+
+            if (file.exists() && !file.delete()) {
+                throw new IOException("Unable to delete file: " + file.toString());
+            }
+            if (!tmpFile.renameTo(file)) {
+                throw new IOException("Unable to rename file " + tmpFile.toString() + " to " + file.toString());
+            }
+            return file;
+        } catch (IOException ignore) {
+            // go on with next repository
+            LOG.debug(Ix2 + "Could not download [" + url + "]");
+            LOG.trace(Ix2 + "Reason [" + ignore.getClass().getName() + ": " + ignore.getMessage() + "]");
         }
-        return tmpFile;
+
+        // no artifact found
+        throw new IOException("URL [" + url + "] could not be resolved.");
     }
 }
